package com.dyh.quickdemozwei;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import sun.misc.Unsafe;

import java.lang.reflect.Field;
import java.util.concurrent.Future;
import java.util.function.Supplier;

import static com.dyh.quickdemozwei.TestHashLock.reflectGetUnsafe;

/*
  Runnable r1 = ()->{
          log.info("耗时操作开始");
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                log.info("InterruptedException!");
            }
            output[0]="dawdawodhawoid";
            log.info("耗时操作结束");
        };
*/
@Slf4j
public class TestInterrupt {



    interface IO{


        void start();

        boolean isComplete();

        void onComplete();
    }

    static class InterchangeThread extends Thread{
        long frame=100L;
        int slot;
        IO[] ios;



        static final Unsafe U;
        static final    long ABASE;//16
        static  final long ASHIFT;

        static {
            try {
                Field field = Unsafe.class.getDeclaredField("theUnsafe");
                field.setAccessible(true);

                U =  (Unsafe) field.get(null);

                Class<?> ak =   IO[].class;
                ABASE = U.arrayBaseOffset(ak);
//                System.out.println("ABASE="+ABASE);

                int scale = U.arrayIndexScale(ak);//4
//                System.out.println("scale="+scale);

                ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);

            }catch (Exception e){
                throw new Error(e);
            }

        }


        public InterchangeThread(int slot) {
            this.slot = slot;
            ios=new IO[slot];
        }

        public void apply(IO io){
            for (;;){
                for (int i = 0;i< slot;i++){
                    if (ios[i]==null){
                        long offset =  ((long)i << ASHIFT) + ABASE;

                        if (U.compareAndSwapObject(ios,offset,null,io)){
                            io.start();
                            return;
                        }

                    }
                }
            }

        }

        @Override
        public void run() {
            for (;;){
                for (int i = 0;i< slot;i++){
                    IO io = ios[i];
                    if (io==null)
                        continue;
                    if (!io.isComplete())
                        continue;
                    io.onComplete();
//                    long offset =  ((long)i << ASHIFT) + ABASE;
//                    U.compareAndSwapObject(ios,offset,null,io);
                    ios[i] = null;
                }

                try {
                    Thread.sleep(frame);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }


    static class DemoIO implements IO{
        String name;
        long time;
        String res;

        public DemoIO(String name, long time) {
            this.name = name;
            this.time = time;
        }

        @Override
        public void start()   {
            log.info("IO name={} 开始",name);
            /*单纯为了模拟IO*/
            new Thread(()->{
                for (;;){
                    try {
                        Thread.sleep(time);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    this.res = "dawkdhawwkduawd";
                    break;
                }
            }).start();

        }

        @Override
        public boolean isComplete() {
            return res!=null;
        }

        @Override
        public void onComplete() {
            log.info("IO name={} 完成! {}",name,res);
        }
    }

    @Test public void test() throws InterruptedException {
        InterchangeThread interchangeThread = new InterchangeThread(3);
        interchangeThread.setName("专门处理io的线程");
        interchangeThread.start();

        interchangeThread.apply(new DemoIO("1s的IO",1000));
        interchangeThread.apply(new DemoIO("3s的IO",3000));
        interchangeThread.apply(new DemoIO("1s的IO-2",1000));
        interchangeThread.apply(new DemoIO("2s的IO",2000));

        interchangeThread.join();
    }

//    class BusySwichException extends RuntimeException{
//
//    }
//
//    @Test
//    public void test() throws InterruptedException {
//        Object[] output = new Object[1];//为了让r1 可以修改
//
//
//        Runnable io1 = ()->{
//            log.info("耗时io操作开始");
//            int i =0;
//            for (;;){
//
//                try {
//                    Thread.sleep(1000L);
//
//                } catch (InterruptedException e) {
//                    log.info("InterruptedException!");//假设这里是不能被打断的IO
//                }
//                if (i++>4)
//                    break;
//            }
//            output[0]="dawdaWdawdawdawd";
//
//            log.info("耗时io操作结束");
//        };
//
//        Runnable r1 = ()->{
//
//            io1.run();
//        };
//
//        Thread t1 = new Thread(r1);
//        t1.setName("t1");
//        t1.start();
//        try {
//            Thread.sleep(1000L);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        t1.interrupt();
//
//
//        t1.join();
//
//        log.info("join over");
//
//        Thread.sleep(5000L);
//
//        log.info("output={}",output[0]);
//    }
}
